package com.telecom.weixi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.MenuItem;

public class BaseActivity extends Activity {
	private static final int MENU_ID_SHARE 	= 0x001;
	private static final int MENU_ID_IDEA  	= 0x002;
	private static final int MENU_ID_ABOUT 	= 0x003;
	private static final int MENU_ID_EXIT  	= 0x004;
	private static final int MENU_ID_PASSWD = 0x005;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuItem shareMenu = menu.add(0, MENU_ID_SHARE, 0, R.string.menu_share);
		shareMenu.setIcon(R.drawable.ic_share);
		
		MenuItem ideaMenu = menu.add(0, MENU_ID_IDEA, 0, R.string.menu_suggest);
		ideaMenu.setIcon(R.drawable.ic_suggust);
		
		MenuItem pwsswdMenu = menu.add(0, MENU_ID_PASSWD, 0, R.string.menu_passwd);
		pwsswdMenu.setIcon(R.drawable.ic_setting);
		
		MenuItem aboutMenu = menu.add(0, MENU_ID_ABOUT, 0, R.string.menu_about);
		aboutMenu.setIcon(R.drawable.ic_about);
		
		MenuItem exitMenu = menu.add(0, MENU_ID_EXIT, 0, R.string.menu_exit);
		exitMenu.setIcon(R.drawable.ic_exit);
		
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);
		boolean ret = false;
		switch(item.getItemId()){
		case MENU_ID_SHARE:
			ret = true;
			startActivity(new Intent(this, ShareActivity.class));
			break;
		case MENU_ID_IDEA:
			ret = true;
			startActivity(new Intent(this, SuggustActivity.class));
			break;
		case MENU_ID_ABOUT:
			ret = true;
			startActivity(new Intent(this, AboutMeActivity.class));
			break;
		case MENU_ID_EXIT:
			ret = true;
			confirmExit();
			break;
		case MENU_ID_PASSWD:
			ret = true;
			modifyPasswd();
			break;
		default:
		}
		return ret;
	}
	
	private void modifyPasswd(){
		Intent intent = new Intent(this, SetPatternActivity.class);
		startActivity(intent);
	}
	private void confirmExit(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	
    	builder.setTitle(getString(R.string.exit_tip));
    	builder.setIcon(getResources().getDrawable(R.drawable.ic_warning));
    	builder.setMessage(null);
    	builder.setPositiveButton(getString(R.string.promp_yes), new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				setResult(WeixiApplication.CODE_EXIT_SFWA);
				BaseActivity.this.finish();
			}
		});
    	builder.setNegativeButton(getString(R.string.promp_no), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
    	builder.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == WeixiApplication.CODE_EXIT_SFWA){
			setResult(resultCode);
			this.finish();
		}
	}
	
}
